<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.qty`" :rules="formRules.qty" class="mb-0px!">
            <el-input v-model="row.qty" placeholder="请输入数量" :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="已使用数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.useQty`" class="mb-0px!">
            <el-input v-model="row.useQty" placeholder="已使用数量" :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="税率" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.irate`" :rules="formRules.irate" class="mb-0px!">
            <el-input v-model="row.irate" placeholder="请输入税率" :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="单价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.price`" :rules="formRules.price" class="mb-0px!">
            <el-input v-model="row.price" placeholder="请输入单价" :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="含税单价" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.itaxprice`" :rules="formRules.itaxprice" class="mb-0px!">
            <el-input v-model="row.itaxprice" placeholder="请输入含税单价" :disabled="true" />
          </el-form-item>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column> -->
    </el-table>
  </el-form>
  <!-- <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round :disabled="true" >+ 添加存货管理子</el-button>
  </el-row> -->
</template>
<script setup lang="ts">
import { StockApi } from '@/api/wms/stock'
const params = reactive({
  inventoryId: undefined,
  warehouseId: undefined,
  locationId: undefined,
})
const props = defineProps<{
  inventoryId?: number // 存货id（主表的关联字段）
  locationId?: number
  warehouseId?: number
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.inventoryId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      params.inventoryId=props.inventoryId
      params.locationId=props.locationId
      params.warehouseId=props.warehouseId
      formData.value = await StockApi.getStockLogListByInventoryId(params)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    inventoryId: undefined,
    qty: undefined,
    irate: undefined,
    price: undefined,
    itaxprice: undefined,
  }
  row.inventoryId = props.inventoryId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>